package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("PurchasematerialController")
public class PurchasematerialController extends CmiEmaController {

	@RequestMapping("/listPurchasematerials")
	public ModelAndView listPurchasematerials(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = purchasematerialPaginationService.updatePagination(request, pagination);
		Set<Purchasematerial> purchasematerials = purchasematerialPaginationService
				.getPaginationPurchasematerials(pagination);

		mav.addObject("purchasematerials", purchasematerials);
		mav.addObject("entityName", "purchasematerial");
		mav.addObject("pagination", pagination);
		mav.setViewName("purchasematerial/listPurchasematerials.jsp");
		return mav;
	}

	public String listPurchasematerials() {
		return "redirect:/listPurchasematerials";
	}

	@RequestMapping("/viewPurchasematerial")
	public ModelAndView viewPurchasematerial(@RequestParam Integer purchasematerialId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("purchasematerial", purchasematerialDAO.findPurchasematerialByPrimaryKey(purchasematerialId));
		mav.setViewName("purchasematerial/viewPurchasematerial.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeletePurchasematerials")
	public ModelAndView confirmDeletePurchasematerials(
			@RequestParam(value = "deletePurchasematerialsIds", required = true) Set<Integer> deletePurchasematerialsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Purchasematerial> deletePurchasematerials = purchasematerialDAO
				.findPurchasematerialsByIds(deletePurchasematerialsIds);
		Set<Purchasematerial> referencedPurchasematerials = purchasematerialService
				.loadReferencePurchasematerials(deletePurchasematerials);
		boolean hasReferenced;
		if (referencedPurchasematerials.size() == 0) {
			mav.addObject("deleteEntitys", deletePurchasematerials);
			hasReferenced = false;
		} else {
			deletePurchasematerials.removeAll(referencedPurchasematerials);
			mav.addObject("referencedEntitys", referencedPurchasematerials);
			mav.addObject("deleteEntitys", deletePurchasematerials);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Purchasematerial");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listPurchasematerials");
		return mav;
	}

	@RequestMapping("/deletePurchasematerial")
	public String deletePurchasematerial(@RequestParam Integer purchasematerialId) {
		Purchasematerial purchasematerial = purchasematerialDAO.findPurchasematerialByPrimaryKey(purchasematerialId);
		purchasematerialService.deletePurchasematerial(purchasematerial);
		return "forward:/listPurchasematerials";
	}

	@RequestMapping("/confirmDeletePurchasematerial")
	public ModelAndView confirmDeletePurchasematerial(@RequestParam Integer purchasematerialId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("purchasematerial", purchasematerialDAO.findPurchasematerialByPrimaryKey(purchasematerialId));

		mav.setViewName("purchasematerial/deletePurchasematerial.jsp");

		return mav;
	}

	@RequestMapping("/deletePurchasematerials")
	public String deletePurchasematerials(
			@RequestParam(value = "deletePurchasematerialsId", required = true) Set<Integer> deletePurchasematerialsId) {
		purchasematerialService.deletePurchasematerials(deletePurchasematerialsId);
		return "forward:/listPurchasematerials";
	}

	@RequestMapping("/editPurchasematerial")
	public ModelAndView editPurchasematerial(@RequestParam Integer purchasematerialId) {

		ModelAndView mav = new ModelAndView();

		Purchasematerial purchasematerial = purchasematerialDAO.findPurchasematerialByPrimaryKey(purchasematerialId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("purchasematerial", purchasematerial);

		// 把所有的引用purchasematerial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(purchasematerialService.loadPurchasematerialObjectReferencedSet(purchasematerial));

		// 将编辑页面purchasematerial添加到mav中
		mav.setViewName("purchasematerial/editPurchasematerial.jsp");
		return mav;
	}

	@RequestMapping("/newPurchasematerial")
	public ModelAndView newPurchasematerial() {
		ModelAndView mav = new ModelAndView();
		Purchasematerial purchasematerial = new Purchasematerial();

		mav.addObject("purchasematerial", purchasematerial);

		// 把所有的引用purchasematerial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(purchasematerialService.loadPurchasematerialObjectReferencedSet(purchasematerial));

		mav.addObject("newFlag", true);
		mav.setViewName("purchasematerial/editPurchasematerial.jsp");

		return mav;
	}

	@RequestMapping("/savePurchasematerial")
	public ModelAndView savePurchasematerial(@ModelAttribute Purchasematerial purchasematerial) {

		ModelAndView mav = new ModelAndView();

		Material materialTemp = purchasematerial.getMaterial();
		Material material = materialTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(materialDAO.findMaterialById(materialTemp.getId()));
		Producer producerTemp = purchasematerial.getProducer();
		Producer producer = producerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(producerDAO.findProducerById(producerTemp.getId()));
		Supplier supplierTemp = purchasematerial.getSupplier();
		Supplier supplier = supplierTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(supplierDAO.findSupplierById(supplierTemp.getId()));
		Unit unitTemp = purchasematerial.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Purchasematerial existingPurchasematerial = CmiHibernateProxyUtil
				.deproxy(purchasematerialDAO.findPurchasematerialByPhysicalPrimaryKey(producer.getId(),
						supplier.getId(), material.getId(), purchasematerial.getDate()));

		purchasematerial.setMaterial(material);
		purchasematerial.setProducer(producer);
		purchasematerial.setSupplier(supplier);
		purchasematerial.setUnit(unit);
		// 新建记录
		if (purchasematerial.getId() == null) {
			if (purchasematerial.equals(existingPurchasematerial)) {

				mav.addAllObjects(purchasematerialService.loadPurchasematerialClassReferencedSet(purchasematerial));
				// mav = editContinue(mav, purchasematerial);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("purchasematerial/editPurchasematerial.jsp");
			} else {
				purchasematerialService.savePurchasematerial(purchasematerial);
				mav.setViewName("forward:/listPurchasematerials");
			}
		}
		// 编辑记录
		else {
			Purchasematerial originPurchasematerial = CmiHibernateProxyUtil
					.deproxy(purchasematerialDAO.findPurchasematerialByPrimaryKey(purchasematerial.getId()));
			if (!purchasematerial.equals(originPurchasematerial) && purchasematerial.equals(existingPurchasematerial)) {
				mav.addAllObjects(purchasematerialService.loadPurchasematerialClassReferencedSet(purchasematerial));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("purchasematerial/editPurchasematerial.jsp");
			} else {
				purchasematerialService.savePurchasematerial(purchasematerial);
				mav.setViewName("forward:/listPurchasematerials");
			}
		}
		return mav;
	}

	@RequestMapping("/purchasematerialController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
